$('.nearestStore').on('tap',function(){
	var w=$(window).width();
	setTimeout(function(){
		dialog('Delete credit card information', 'Cannot find nearest store. Make sure your location service has turn on.And press &apos; Ok &apos;if your  evice ask to use the location services.', 3);
	    var $dialog = $('#dialog');
	    $dialog.show();
	    $('#giveBtn').html('Select by district');
	    $('#comfirmBtn').html('Refresh Again');
	    if(w<375){
		    $('.weui_dialog_ft a').css({'padding':'0 4px','margin':'0 3px'});
	    }else{
		    $('.weui_dialog_ft a').css({'padding':'0 10px','margin':'0 8px'});
	    }
	    $('.mui-icon-closeempty').hide();
	    $dialog.find('#comfirmBtn').one('click', function() {
	        $dialog.hide();
	        $dialog.remove();
			location.href=MPJ.base+"/pages/store/find-a-store.jsp";
	});
	},3000)
});

var mapData = {};
$().ready(function() {

	$.ajax({
		type: "get",
		url: MPJ.apiBase + '/mpj2shop/docroot/en/district_cnc.xml',
		dataType: "xml",
		async: true,
		//data: {
		//    url: "http://www.marketplacebyjasons.com/mpjshop/docroot/en/das.xml"
		//},
		success: function (obj) {
			console.log(obj);

			mapData.region = new Array();
			mapData.district = new Array();
			$(obj).find('REGION').each(function (i) {
				var region = {
					name : $(this).children('Name').text(),
					id: $(this).attr('id')
				};

				mapData.region.push(region);

				$(this).find('DISTRICT').each(function (k) {
					var district = {};
					district.id = $(this).attr('id');
					district.name = $(this).children('Name').text();
					district.region = region.id;
					mapData.district.push(district);
				});

			});

			console.log(mapData);
			buildRegion();
		},
		error: function () {

		}

	});

	function buildRegion() {
		for (var i = 0; i < mapData.region.length; i ++) {
			var $region = mapData.region[i];
			var $select = $("<select id='" + $region.id + "'></select>");
			$select.append("<option> -- " + $region.name + " --</option>");
			$(".store-main").append($select);

			for (var j = 0; j < mapData.district.length; j ++) {
				var $district = mapData.district[j];
				if ($district.region == $region.id) {
					$select.append("<option id='" + $district.id + "'>" + $district.name + "</option>");
				}
			}
		}
	}

});